package com.module.overview.ui.cardHistoryDetails.provider

import android.view.View
import android.widget.TextView
import com.chad.library.adapter.base.provider.BaseItemProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.common.app.core.RouterHub
import com.common.app.data.bean.KeyBundle
import com.common.app.data.bean.overView.HistoryDetailsModel
import com.common.app.helper.RouterHelper
import com.common.app.utls.TimeUtils
import com.common.app.widget.SegmentProgressBar
import com.common.base.app.extras.doOnIOThread
import com.common.base.app.extras.doOnUiThread
import com.common.base.app.extras.setOnClickListener
import com.common.base.utils.ViewUtils.getString
import com.common.base.widget.CustomTextView
import com.module.ble.repo.HeartRateVariabilityRepo
import com.module.ble.repo.UserStressRepo
import com.module.ble.utils.AllCardTypeConfig
import com.module.overview.R

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2025/7/10
 *
 * 描述：压力状态卡片
 *
 * 修订历史：完成度100%
 *
 */
class OvPressureStateValueCardProvider(time:Long,
    override val itemViewType: Int = AllCardTypeConfig.TYPE_CARD_PRESSURE_STATE_VALUE,
    override val layoutId: Int = R.layout.overview_pressure_state_chart_crad_provider
) : BaseItemProvider<HistoryDetailsModel>() {

    private var curTime = time

    private var segmentProgressBar:SegmentProgressBar? = null
    private var tvStressValueStr:TextView? = null
    private var tvStressValue:TextView? = null
    private var tvNewHrv:TextView? = null
    private var tvTheDayHrv:TextView? = null

    init {

    }

    override fun onViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {

        viewHolder.getView<CustomTextView>(R.id.tvStressValueStr).setOnClickListener(onClick = object : View.OnClickListener {
            override fun onClick(p0: View?) {
                RouterHelper.navigationPostcard(RouterHub.ROUTER_HOME_SCORING_FACTORS)
                    .withInt(KeyBundle.BUNDLE_KEY, 0)
                    .withString(KeyBundle.EXPAND_DATA, TimeUtils.getYearMonthDay(curTime))
                    .navigation()
            }
        })

        segmentProgressBar = viewHolder.getView<SegmentProgressBar>(R.id.segmentProgressBar).apply {
            // 设置4段，比例分别为 30%, 20%, 25%, 25%
            setSegmentRatios(floatArrayOf(0.3f, 0.3f, 0.2f, 0.2f))
        }
        tvStressValueStr = viewHolder.getView(R.id.tvStressValueStr)
        tvStressValue = viewHolder.getView<TextView>(R.id.tvStressValue).apply {
            text = "${getString(R.string.overview_stress_value_hint)}${getString(R.string.common_rail_unit)}"
        }
        tvNewHrv = viewHolder.getView(R.id.tvNewHrv)
        tvTheDayHrv = viewHolder.getView(R.id.tvTheDayHrv)

        super.onViewHolderCreated(viewHolder, viewType)
    }

    override fun convert(helper: BaseViewHolder, item: HistoryDetailsModel) {

        getTheDayStress()
    }

    /**
     * 获取当天压力
     */
    private fun getTheDayStress(){
        if (curTime <=0) return

        doOnIOThread {
            //当天压力
            UserStressRepo.instance.findByTimestampTheDay(curTime)?.let { originalListIt->
                if (originalListIt.isNullOrEmpty()) return@let
                val theDayAverage = if (originalListIt.isNotEmpty()) {//当日平均
                    originalListIt.map { it.stress }.average().toInt()
                } else {
                    0
                }

                val stressValueStr = when(theDayAverage){
                    in 0..30 -> getString(R.string.common_pressure_relax_hint)
                    in 31..60 -> getString(R.string.common_pressure_normal_hint)
                    in 61..80 -> getString(R.string.common_pressure_medium_hint)
                    in 81..100 -> getString(R.string.common_pressure_high_hint)
                    else -> getString(R.string.common_rail_unit) // 处理超出范围的情况
                }
                doOnUiThread {
                    tvStressValueStr?.text = stressValueStr
                    tvStressValue?.text = "${getString(R.string.overview_stress_value_hint)} $theDayAverage"
                    segmentProgressBar?.setProgress(theDayAverage)
                }

            }

            //最新HRV
            HeartRateVariabilityRepo.instance.findLatestByDayWithHrv(curTime)?.let { originalIt->
                doOnUiThread {
                    tvNewHrv?.text = originalIt.heartRateVariability.toString()
                }
            }

            //当天所有HRV
            HeartRateVariabilityRepo.instance.findAllByDayWithHrv(curTime)?.let { originalListIt->
                if (originalListIt.isNullOrEmpty()) return@let
                val theDayAverage = if (originalListIt.isNotEmpty()) {//当日平均
                    originalListIt.map { it.heartRateVariability }.average().toInt()
                } else {
                    0
                }
                doOnUiThread {
                    tvTheDayHrv?.text = theDayAverage.toString()
                }
            }
        }
    }

}